package com.infore.common.response;

import com.alibaba.fastjson.annotation.JSONField;
import com.infore.common.enums.BaseError;
import com.infore.common.enums.ErrorCode;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.List;

@Data
@EqualsAndHashCode(callSuper = false)
public class ApiListResponse<T> extends ApiResponse {

    @JSONField(ordinal = 5)
    @ApiModelProperty(value = "返回数据")
    private List<T> data;


    @JSONField(ordinal = 6)
    @ApiModelProperty(value = "列头")
    private List<String> head;


    public List<T> getData() {
        return data;
    }

    public ApiListResponse<T> setData(List<T> dataList) {
        this.data = dataList;
        return this;
    }

    public static ApiListResponse error(String code, String msg) {
        ApiListResponse response = new ApiListResponse();
        response.setSuccessful(false);
        response.setCode(code);
        response.setMsg(msg);
        return response;
    }

    public static ApiListResponse error(ErrorCode errorCode) {
       return  error(errorCode.getCode(),errorCode.getMsg());
    }



    public static ApiListResponse ok(String msg) {
        ApiListResponse response = new ApiListResponse();
        response.setSuccessful(true);
        response.setCode(BaseError.OK_KEY_VALUE.getCode());
        response.setMsg(msg);
        return response;
    }

    public static <T> ApiListResponse<T> ok(List<T> dataList) {
        ApiListResponse response = ok("OK");
        response.setData(dataList);
        return response;
    }
    public static <T> ApiListResponse<T> ok(List<T> dataList,List<String> head) {
        ApiListResponse response = ok("OK");
        response.setData(dataList);
        response.setHead(head);
        return response;
    }
    public static <T> ApiListResponse<T> ok(List<T> dataList,String  msg, List<String> head) {
        ApiListResponse response = ok(msg);
        response.setData(dataList);
        response.setHead(head);
        return response;
    }

}
